using Microsoft.AspNetCore.Mvc;
using SportsStore.Models;
using SportsStore.Models.ViewModels;

namespace SportsStore.Controllers
{
    public class HomeController : Controller
    {
        private readonly IStoreRepository storeRepository;
        public int PageSize = 4;
        public HomeController(IStoreRepository repo)
        {
            this.storeRepository = repo;
        }
        // GET: HomeController
        public ViewResult Index(string? category, int productPage = 1) =>

            View(new ProductsListViewModel
            {
                Products = storeRepository.Products.Where(p => category == null || p.Category == category).OrderBy(p => p.ProductID).Skip((productPage - 1) * PageSize).Take(PageSize),
                PagingInfo = new PagingInfo { CurrentPage = productPage, ItemsPerPage = PageSize, TotalItems = storeRepository.Products.Count() },
                CurrentCategory = category
            });
    }
}
